﻿using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace PineLake.Drmwe.Core.AutoFac
{
    public class AutofacModule : Autofac.Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            var basePath = AppContext.BaseDirectory;

            #region 带有接口层的服务注入

            var ServicesDllFile = Path.Combine(basePath, "MultipleDB.Services.dll");
            var RepositoryDllFile = Path.Combine(basePath, "MultipleDB.Repository.dll");
            var EncryptsDllFile = Path.Combine(basePath, "PineLake.Drmwe.Encrypts.dll");

            if (!(File.Exists(ServicesDllFile) && File.Exists(RepositoryDllFile)&& File.Exists(EncryptsDllFile)))
            {
                var msg = "PineLake.XX.Repository.dll和PineLake.XX.Services.dll 和 PineLake.Drmwe.Encrypts.dll 丢失,项目解耦,请检查 bin 文件夹,并复制!";
                throw new Exception(msg);
            }

            // 获取 Service.dll 程序集服务，并注册
            var assemblysServices = Assembly.LoadFrom(ServicesDllFile);
            //支持属性注入依赖重复
            builder.RegisterAssemblyTypes(assemblysServices).AsImplementedInterfaces().InstancePerDependency()
                .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);

            // 获取 Repository.dll 程序集服务，并注册
            var assemblysRepository = Assembly.LoadFrom(RepositoryDllFile);
            //支持属性注入依赖重复
            builder.RegisterAssemblyTypes(assemblysRepository).AsImplementedInterfaces().InstancePerDependency()
                .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);

            // 获取 Encrypts.dll 程序集服务，并注册
            var assemblysEncrypts = Assembly.LoadFrom(EncryptsDllFile);
            //支持属性注入依赖重复
            builder.RegisterAssemblyTypes(assemblysEncrypts).AsImplementedInterfaces().InstancePerDependency()
                .PropertiesAutowired(PropertyWiringOptions.AllowCircularDependencies);
            #endregion

        }
    }
}
